package cn.formaggie.controller.manage;

import cn.formaggie.annotation.CheckTwiceSubmit;
import cn.formaggie.controller.BaseController;
import cn.formaggie.entity.model.PageBean;
import cn.formaggie.entity.model.store.Handing;
import cn.formaggie.entity.model.store.StoreItem;
import cn.formaggie.service.face.HandingService;
import cn.formaggie.service.face.StoreItemService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/back/handing")
public class HandingManageController extends BaseController {

    @Autowired
    private HandingService handingService;

    @RequiresPermissions({"加工方信息管理"})
    @RequestMapping("/list")
    public ModelAndView list(@ModelAttribute(STATE_MSG)String stateMsg){
        ModelAndView mav=new ModelAndView();
        mav.addObject(STATE_MSG,stateMsg);
        mav.addObject("pageName","加工方信息管理");
        mav.addObject("mainPage",BACK_PAGE_PATH+"/manager/store/handing/list.jsp");
        mav.setViewName("/backpage/backTemp");
        return mav;
    }

    @RequiresPermissions({"加工方信息管理"})
    @RequestMapping("/load")
    @ResponseBody
    public Map load(@RequestParam(value = "s_name",required = false)String name,
                    @RequestParam(value = "page") Integer page,
                    @RequestParam(value = "limit") Integer limit){
        PageBean pageBean=new PageBean(page,limit);
        Map<String,Object> sqlMap=new HashMap<>();
        sqlMap.put("name",name);
        sqlMap.put("pageBean",pageBean);

        List<Handing> data=handingService.findHandings(sqlMap);
        Integer count=handingService.getHandingsCount(sqlMap);

        Map<String,Object> viewMap=new HashMap<>();
        viewMap.put("data",data);
        viewMap.put("count",count);
        viewMap.put("code",0);
        viewMap.put("msg","");
        return viewMap;
    }

    @RequiresPermissions({"加工方信息管理"})
    @RequestMapping("/add")
    @CheckTwiceSubmit
    public String addSampleType(Handing handing, RedirectAttributes model){
        Boolean state=handingService.addHanding(handing);
        model.addFlashAttribute(STATE_MSG,state?"添加成功":"添加失败");
        return "redirect:/back/handing/list";
    }

    @RequiresPermissions({"加工方信息管理"})
    @RequestMapping("/modifyInfo")
    @CheckTwiceSubmit
    public String modifySampleTypeInfo(Handing handing, RedirectAttributes model){
        Boolean state=handingService.modifyHandingInfo(handing);
        model.addFlashAttribute(STATE_MSG,state?"修改成功":"修改失败");
        return "redirect:/back/handing/list";
    }

}
